#include "point.h"

point::point() : x(0.0), y(0.0)
{
}

point::point(const double x_, const double y_) : x(x_), y(y_)
{
}

point::point(const point& another)
{
	x = another.x;
	y = another.y;
}

point& point::operator =(const point& another)
{
	x = another.x;
	y = another.y;
	return *this;
}

mpq_class distance2(const point& p1, const point& p2)
{
	return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}

bool cmp_by_x::operator()(const point& p1, const point& p2) const
{
	return p1.x < p2.x;
}

cmp_by_angle::cmp_by_angle() : center(point(0, 0))
{
}

cmp_by_angle::cmp_by_angle(const point& another) : center(another)
{
}

bool cmp_by_angle::operator() (const point& p1, const point& p2) const
{
	sign result = left_turn(p1, center, p2);
	if (result == sign::NEGATIVE)
		return true;
	else
	if (result == sign::POSITIVE)
		return false;
	else
		return distance2(p1, center) < distance2(p2, center);
}

sign left_turn(const point& p1, const point& p2, const point& p3)
{
	mpq_class result = (p1.x - p2.x) * (p3.y - p2.y) - (p3.x - p2.x) * (p1.y - p2.y);
	if (result < 0)
		return sign::NEGATIVE;	
	else
	if (result > 0)
		return sign::POSITIVE;
	else
		return sign::ZERO;
}
